<?php
function VoteDataCenter_adminform_main (){
  VoteDataCenter_adminform_submit ();
}

function VoteDataCenter_adminform_submit ()
{
    //$date_time = getdate();
    $forward =  FormUtil::getPassedValue ('forward', null);
    $ctrl =  FormUtil::getPassedValue ('ctrl', null);
    $form = FormUtil::getPassedValue ('form', null);
    $id = FormUtil::getPassedValue ('id', null);

    $is_array = FormUtil::getPassedValue ('array', false);
    if ($is_array){
      $is_array = true;
    }else{
      $is_array = false;
    }
    if (empty($ctrl)){
      if ($form[ctrl]){
        $ctrl = $form[ctrl];      
      }else{
        return 'ERROR VoteDataCenter system can not find controller variable';
      }
    }


    $list_url = pnModURL('VoteDataCenter', 'admin', 'list' , array('ctrl'   => $ctrl,
                                                                   'form[pro_id]' => $form['pro_id'],
                                                                   'form[dis_id]' => $form['dis_id'],
                                                                   'form[tam_id]' => $form['tam_id']
                                                        )
                        );


    if (isset($_POST['button_cancel']) || isset($_POST['button_cancel_x'])){
        pnRedirect($list_url);
        return true;
    }

    if (!($class = Loader::loadClassFromModule ('VoteDataCenter', $ctrl , $is_array))){
        return LogUtil::registerError ("Unable to load class [$ctrl] ...");
    }
    $object = new $class ();
    $object->getDataFromInput ('form',null,'POST');

    if ($_POST['button_delete'] || $_POST['button_delete_x']){
        $object->delete ();
    }else{
        $object->save ();
    }
    if (method_exists($object,'genForward')){
      $forwar_url = $object->genForward();
      if (!empty($forward_url)){
        pnRedirect($forward_url);
      }
    }else if (count($forward) > 0 && $forward[id]){
      $forward_url = generateUrl($forward);
      pnRedirect($forward_url);
    }else{
      pnRedirect($list_url);
    }
    
    return true;
}


/**
* @param $urlarray  the array to generate contain key and value
*                   ex.  array('modname'=>'VoteDataCenter'
                               'func'   =>'save'
                               'type'   =>'admin');
*/
function generateUrl($urlarray){
  //default value

  $forward =  FormUtil::getPassedValue ('forward', null);

  $modname = 'VoteDataCenter';

  if($forward['func']){
    $func = $forward['func'];
  }else{
    $func = 'list';
  }

  if($forward['id']){
    $param[] = 'id='.$forward['id'];
  }

  $type = 'admin';

  $param = array();
  foreach($urlarray as $key => $value){
    if (strcmp(strtolower($key),'modname') === 0){
      $modname = $value;
    }else if (strcmp(strtolower($key),'func') === 0){
      $func = $value;
    }else if (strcmp(strtolower($key),'type') === 0){
      $type = $value;
    }else{
      $param[] = $key . '=' .$value;
    }
  }
  return "index.php?module=$modname&func=$func&type=$type&" . implode('&',$param);
}



function VoteDataCenter_adminform_compress () {
    //Get data from page
    $ctrl =  FormUtil::getPassedValue ('ctrl', null);
    $form = FormUtil::getPassedValue ('form', null);

    $pntables  = pnDBGetTables();

    /////////////////////////////////////////////////////////////////
    //Query province data from database & Write province data to csv file
    /////////////////////////////////////////////////////////////////
    //Query province data
    $provinceTable = $pntables['votedatacenter_province'];
    $provinceColumn = $pntables['votedatacenter_province_column'];
    $provinceArray = array();
    if($form[pro_id]  !== '0'){
      $where = " WHERE  $provinceTable.$provinceColumn[id] = $form[pro_id] ";
    }else{
      $where = " ";
    }
    $sql = "
            SELECT
              $provinceTable.$provinceColumn[id],
              $provinceTable.$provinceColumn[name],
              $provinceTable.$provinceColumn[latitude],
              $provinceTable.$provinceColumn[longitude]
            FROM
              $provinceTable
            ".$where;

    $column = array('id', 'name', 'latitude', 'longitude');
    $result = DBUtil::executeSQL($sql);
    $provinceArray = DBUtil::marshallObjects ($result, $column);

    // Write province data
    //ksort($provinceArray);
    $csv_file = "pnTemp/votedatacenter_upload/province.csv";
    $fp = fopen($csv_file, "w");
    foreach($provinceArray as $key => $data) {
      $write .= $data['id'] . ','.$data['name']. ','.$data['latitude']. ','.$data['longitude']."\r\n";
    }
    fwrite($fp, $write);
    fclose($fp);
    unset($csv_file);
    unset($provinceArray);
    unset($fp);
    unset($write);

    /////////////////////////////////////////////////////////////////
    //Query district data from database & Write district data to csv file
    /////////////////////////////////////////////////////////////////
    //Query district data
    $districtTable = $pntables['votedatacenter_district'];
    $districtColumn = $pntables['votedatacenter_district_column'];
    $districtArray = array();
    if($form[pro_id]  !== '0' && $form[dis_id]  !== '0'){
      $where = " WHERE 
                   $districtTable.$districtColumn[pro_id] = $form[pro_id] 
                  AND
                   $districtTable.$districtColumn[id] = $form[dis_id] ";
    }else if( $form[pro_id]  !== '0'){
      $where = " WHERE 
                   $districtTable.$districtColumn[pro_id] = $form[pro_id] ";
    }else {
      $where = " ";
    }
    $sql = "
            SELECT
              $districtTable.$districtColumn[id],
              $districtTable.$districtColumn[pro_id],
              $districtTable.$districtColumn[name],
              $districtTable.$districtColumn[latitude],
              $districtTable.$districtColumn[longitude]
            FROM
              $districtTable
            ".$where;

    $column = array('id', 'pro_id','name', 'latitude', 'longitude');
    $result = DBUtil::executeSQL($sql);
    $districtArray = DBUtil::marshallObjects ($result, $column);

    // Write district data
    //ksort($districtArray);
    $csv_file = "pnTemp/votedatacenter_upload/district.csv";
    $fp = fopen($csv_file, "w");
    foreach($districtArray as $key => $data) {
      $write .= $data['id'] . ','.$data['pro_id'] . ','.$data['name']. ','.$data['latitude']. ','.$data['longitude']."\r\n";
    }
    fwrite($fp, $write);
    fclose($fp);
    unset($csv_file);
    unset($districtArray);
    unset($fp);
    unset($write);


    /////////////////////////////////////////////////////////////////
    //Query tambon data from database & Write tambon data to csv file
    /////////////////////////////////////////////////////////////////
    //Query tambon data
    $tambonTable = $pntables['votedatacenter_tambon'];
    $tambonColumn = $pntables['votedatacenter_tambon_column'];
    $tambonArray = array();
    if($form[pro_id] !== '0' && $form[dis_id] !== '0' && $form[tam_id] !== '0' ){
      $where = " WHERE 
                   $tambonTable.$tambonColumn[pro_id] = $form[pro_id] 
                  AND
                   $tambonTable.$tambonColumn[dis_id] = $form[dis_id] 
                  AND
                   $tambonTable.$tambonColumn[id] = $form[tam_id] ";

    }else if($form[pro_id]  !== '0' && $form[dis_id]  !== '0'){
      $where = " WHERE 
                   $tambonTable.$tambonColumn[pro_id] = $form[pro_id] 
                  AND
                   $tambonTable.$tambonColumn[dis_id] = $form[dis_id] ";
    }else if( $form[pro_id]  !== '0'){
      $where = " WHERE 
                   $tambonTable.$tambonColumn[pro_id] = $form[pro_id] ";
    }else {
      $where = " ";
    }
    $sql = "
            SELECT
              $tambonTable.$tambonColumn[id],
              $tambonTable.$tambonColumn[pro_id],
              $tambonTable.$tambonColumn[dis_id],
              $tambonTable.$tambonColumn[name],
              $tambonTable.$tambonColumn[latitude],
              $tambonTable.$tambonColumn[longitude]
            FROM
              $tambonTable
            ".$where;

    $column = array('id', 'pro_id', 'dis_id', 'name', 'latitude', 'longitude');
    $result = DBUtil::executeSQL($sql);
    $districtArray = DBUtil::marshallObjects ($result, $column);

    // Write district data
    //ksort($districtArray);
    $csv_file = "pnTemp/votedatacenter_upload/tambon.csv";
    $fp = fopen($csv_file, "w");
    foreach($districtArray as $key => $data) {
      $write .= $data['id'] . ','.$data['pro_id']  . ','.$data['dis_id'] . ','.$data['name']. ','.$data['latitude']. ','.$data['longitude']."\r\n";
    }
    fwrite($fp, $write);
    fclose($fp);
    unset($csv_file);
    unset($districtArray);
    unset($fp);
    unset($write);

    /////////////////////////////////////////////////////////////////
    //Query village data from database & Write village data to csv file
    /////////////////////////////////////////////////////////////////
    //Query tambon data
    $villageTable = $pntables['votedatacenter_village'];
    $villageColumn = $pntables['votedatacenter_village_column'];
    $villageArray = array();
    if($form[pro_id] !== '0' && $form[dis_id] !== '0' && $form[tam_id] !== '0' ){
      $where = " WHERE 
                   $villageTable.$villageColumn[pro_id] = $form[pro_id] 
                  AND
                   $villageTable.$villageColumn[dis_id] = $form[dis_id] 
                  AND
                   $villageTable.$villageColumn[tam_id] = $form[tam_id] ";

    }else if($form[pro_id]  !== '0' && $form[dis_id]  !== '0'){
      $where = " WHERE 
                   $villageTable.$villageColumn[pro_id] = $form[pro_id] 
                  AND
                   $villageTable.$villageColumn[dis_id] = $form[dis_id] ";
    }else if( $form[pro_id]  !== '0'){
      $where = " WHERE 
                   $villageTable.$villageColumn[pro_id] = $form[pro_id] ";
    }else {
      $where = " ";
    }

    $sql = "
            SELECT
              $villageTable.$villageColumn[id],
              $villageTable.$villageColumn[pro_id],
              $villageTable.$villageColumn[dis_id],
              $villageTable.$villageColumn[tam_id],
              $villageTable.$villageColumn[moo],
              $villageTable.$villageColumn[name],
              $villageTable.$villageColumn[latitude],
              $villageTable.$villageColumn[longitude]
            FROM
              $villageTable
            ".$where;

    $column = array('id', 'pro_id', 'dis_id', 'tam_id',  'moo', 'name', 'latitude', 'longitude');
    $result = DBUtil::executeSQL($sql);
    $villageArray = DBUtil::marshallObjects ($result, $column);

    // Write district data
    //ksort($districtArray);
    $csv_file = "pnTemp/votedatacenter_upload/village.csv";
    $fp = fopen($csv_file, "w");
    foreach($villageArray as $key => $data) {
      $write .= $data['id'] . ','.$data['pro_id']  . ','.$data['dis_id'] . ','.$data['tam_id']. ','.$data['moo'] . ','.$data['name']. ','.$data['latitude']. ','.$data['longitude']."\r\n";
    }
    fwrite($fp, $write);
    fclose($fp);
    unset($csv_file);
    unset($villageArray);
    unset($fp);
    unset($write);

    /////////////////////////////////////////////////////////////////
    //Query house no. data from database & Write house no. data to csv file
    /////////////////////////////////////////////////////////////////
    //Query tambon data
    $houseNoTable = $pntables['votedatacenter_house_no'];
    $houseNoColumn = $pntables['votedatacenter_house_no_column'];
    $houseNoArray = array();
    if($form[pro_id] !== '0' && $form[dis_id] !== '0' && $form[tam_id] !== '0' ){
      $where = " WHERE 
                   $houseNoTable.$houseNoColumn[pro_id] = $form[pro_id] 
                  AND
                   $houseNoTable.$houseNoColumn[dis_id] = $form[dis_id] 
                  AND
                   $houseNoTable.$houseNoColumn[tam_id] = $form[tam_id] ";

    }else if($form[pro_id]  !== '0' && $form[dis_id]  !== '0'){
      $where = " WHERE 
                   $houseNoTable.$houseNoColumn[pro_id] = $form[pro_id] 
                  AND
                   $houseNoTable.$houseNoColumn[dis_id] = $form[dis_id] ";
    }else if( $form[pro_id]  !== '0'){
      $where = " WHERE 
                   $houseNoTable.$houseNoColumn[pro_id] = $form[pro_id] ";
    }else {
      $where = " ";
    }
    $sql = "
            SELECT
              $houseNoTable.$houseNoColumn[id],
              $houseNoTable.$houseNoColumn[pro_id],
              $houseNoTable.$houseNoColumn[dis_id],
              $houseNoTable.$houseNoColumn[tam_id],
              $houseNoTable.$houseNoColumn[vil_id],
              $houseNoTable.$houseNoColumn[vil_moo],
              $houseNoTable.$houseNoColumn[no],
              $houseNoTable.$houseNoColumn[unit],
              $houseNoTable.$houseNoColumn[trok],
              $houseNoTable.$houseNoColumn[soi],
              $houseNoTable.$houseNoColumn[road],
              $houseNoTable.$houseNoColumn[zipcode],
              $houseNoTable.$houseNoColumn[latitude],
              $houseNoTable.$houseNoColumn[longitude]
            FROM
              $houseNoTable
            ".$where;

    $column = array('id', 'pro_id', 'dis_id', 'tam_id','vil_id', 'vil_moo', 'no', 'unit', 'trok', 'soi' , 'road', 'zipcode', 'latitude', 'longitude');
    $result = DBUtil::executeSQL($sql);
    $houseNoArray = DBUtil::marshallObjects ($result, $column);

    // Write district data
    //ksort($districtArray);
    $csv_file = "pnTemp/votedatacenter_upload/house_no.csv";
    $fp = fopen($csv_file, "w");
    foreach($houseNoArray as $key => $data) {
      $write .= $data['id'] . ','.$data['pro_id']  . ','.$data['dis_id'] . ','.$data['tam_id']. ','.$data['vil_id'] . ','.$data['vil_moo'] . ','.$data['no'] . ','.$data['unit'] . ','.$data['trok'] . ','.$data['soi'] . ','.$data['road'] . ','.$data['zipcode'] . ','.$data['latitude']. ','.$data['longitude']."\r\n";
    }
    fwrite($fp, $write);
    fclose($fp);
    unset($csv_file);
    unset($houseNoArray);
    unset($fp);
    unset($write);

    /////////////////////////////////////////////////////////////////
    //Query person. data from database & Write house no. data to csv file
    /////////////////////////////////////////////////////////////////
    //Query person data
    $personTable = $pntables['votedatacenter_person'];
    $personColumn = $pntables['votedatacenter_person_column'];
    $personArray = array();
    if($form[pro_id] !== '0' && $form[dis_id] !== '0' && $form[tam_id] !== '0' ){
      $where = " WHERE 
                   $personTable.$personColumn[pro_id] = $form[pro_id] 
                  AND
                   $personTable.$personColumn[dis_id] = $form[dis_id] 
                  AND
                   $personTable.$personColumn[tam_id] = $form[tam_id] ";

    }else if($form[pro_id]  !== '0' && $form[dis_id]  !== '0'){
      $where = " WHERE 
                   $personTable.$personColumn[pro_id] = $form[pro_id] 
                  AND
                   $personTable.$personColumn[dis_id] = $form[dis_id] ";
    }else if( $form[pro_id]  !== '0'){
      $where = " WHERE 
                   $personTable.$personColumn[pro_id] = $form[pro_id] ";
    }else {
      $where = " ";
    }
    $sql = "
            SELECT
              $personTable.$personColumn[id],
              $personTable.$personColumn[pro_id],
              $personTable.$personColumn[dis_id],
              $personTable.$personColumn[tam_id],
              $personTable.$personColumn[vil_id],
              $personTable.$personColumn[vil_moo],
              $personTable.$personColumn[hou_id],
              $personTable.$personColumn[gen_id],
              $personTable.$personColumn[pid],
              $personTable.$personColumn[firstname],
              $personTable.$personColumn[lastname],
              $personTable.$personColumn[telephone],
              $personTable.$personColumn[mobilephone],
              $personTable.$personColumn[birthdate]
            FROM
              $personTable
            ".$where;


    $column = array('id', 'pro_id', 'dis_id', 'tam_id', 'vil_id', 'vil_moo', 'hou_id', 'gen_id', 'pid', 'firstname' , 'lastname', 'telephone', 'mobilephone', 'birthdate');
    $result = DBUtil::executeSQL($sql);
    $personArray = DBUtil::marshallObjects ($result, $column);

    // Write district data
    //ksort($districtArray);
    $csv_file = "pnTemp/votedatacenter_upload/person.csv";
    $fp = fopen($csv_file, "w");
    foreach($personArray as $key => $data) {
      $write .= $data['id'] . ','.$data['pro_id']  . ','.$data['dis_id'] . ','.$data['tam_id']. ','.$data['vil_id'] . ','.$data['vil_moo'] . ','.$data['hou_id'] . ','.$data['gen_id'] . ','.$data['pid'] . ','.$data['firstname'] . ','.$data['lastname'] . ','.$data['telephone'] . ','.$data['mobilephone'] . ','.$data['birthdate'] ."\r\n";
    }
    fwrite($fp, $write);
    fclose($fp);
    unset($csv_file);
    unset($personArray);
    unset($fp);
    unset($write);


    /////////////////////////////////////////////
    //Query max project id
    /////////////////////////////////////////////
    $pntables  = pnDBGetTables();
    $projectColumn = $pntables['votedatacenter_project_column'];
    $projectarray = array();
    $sql = "
            SELECT
              MAX($pntables[votedatacenter_project].$projectColumn[id])
            FROM
              $pntables[votedatacenter_project]
            ";
    $column = array('id');
    $result = DBUtil::executeSQL($sql);
    $projectarray = DBUtil::marshallObjects ($result, $column);

    if(empty($projectarray[0])){
      $nextID = 1;
    }else{
      $nextID = $projectarray[0]['id']+1;
    }


    /////////////////////////////////////////////
    //Query insert project data
    /////////////////////////////////////////////
    $filename = "vote".$form['pro_id']."".$form['dis_id']."".$form['tam_id']."".$nextID.".zip";
    //var_dump($filename);
    //exit;
    $obj = array(
                  'id'       => $nextID,
                  'pro_id'   => $form['pro_id'],
                  'dis_id'   => $form['dis_id'],
                  'tam_id'   => $form['tam_id'],
                  'filename' => $filename
                );

    $result = DBUtil::insertObject($obj, 'votedatacenter_project');


    /////////////////////////////////////////////////
    //Generate compress project & create new modules
    /////////////////////////////////////////////////
    Loader::loadClass('DZip', "modules/VoteDataCenter/pnincludes");
    $zip = new DZip($filename);

    // Add file to root folder
    $zip->addDir("Votedata"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnadmin.php", "Votedata/pnadmin.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnadminform.php", "Votedata/pnadminform.php");
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnajax.php", "Votedata/pnajax.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pninit.php", "Votedata/pninit.php");
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntables.php", "Votedata/pntables.php");
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnuser.php", "Votedata/pnuser.php");
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnuserform.php", "Votedata/pnuserform.php");
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnversion.php", "Votedata/pnversion.php");
    // Add classes Folder
    $zip->addDir("Votedata/classes"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNAjaxDistrictArray.class.php", "Votedata/classes/PNAjaxDistrictArray.class.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNAjaxHouseNoArray.class.php", "Votedata/classes/PNAjaxHouseNoArray.class.php");
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNAjaxTambonArray.class.php", "Votedata/classes/PNAjaxTambonArray.class.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNAjaxVillageArray.class.php", "Votedata/classes/PNAjaxVillageArray.class.php");
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNDistrict.class.php", "Votedata/classes/PNDistrict.class.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNDistrictArray.class.php", "Votedata/classes/PNDistrictArray.class.php");
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNGender.class.php", "Votedata/classes/PNGender.class.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNGenderArray.class.php", "Votedata/classes/PNGenderArray.class.php");
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNHouseNo.class.php", "Votedata/classes/PNHouseNo.class.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNHouseNoArray.class.php", "Votedata/classes/PNHouseNoArray.class.php");
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNPerson.class.php", "Votedata/classes/PNPerson.class.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNPersonArray.class.php", "Votedata/classes/PNPersonArray.class.php");
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNProject.class.php", "Votedata/classes/PNProject.class.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNProjectArray.class.php", "Votedata/classes/PNProjectArray.class.php");
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNProvince.class.php", "Votedata/classes/PNProvince.class.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNProvinceArray.class.php", "Votedata/classes/PNProvinceArray.class.php");
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNTambon.class.php", "Votedata/classes/PNTambon.class.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNTambonArray.class.php", "Votedata/classes/PNTambonArray.class.php");
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNVillage.class.php", "Votedata/classes/PNVillage.class.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNVillageArray.class.php", "Votedata/classes/PNVillageArray.class.php");
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNMigrate.class.php", "Votedata/classes/PNMigrate.class.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/classes/PNMigrateArray.class.php", "Votedata/classes/PNMigrateArray.class.php");

    // Add pntemplates Folder
    $zip->addDir("Votedata/pntemplates"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/admin_form_district.htm", "Votedata/pntemplates/admin_form_district.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/admin_form_houseno.htm", "Votedata/pntemplates/admin_form_houseno.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/admin_form_person.htm", "Votedata/pntemplates/admin_form_person.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/admin_form_project.htm", "Votedata/pntemplates/admin_form_project.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/admin_form_province.htm", "Votedata/pntemplates/admin_form_province.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/admin_form_tambon.htm", "Votedata/pntemplates/admin_form_tambon.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/admin_form_village.htm", "Votedata/pntemplates/admin_form_village.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/admin_header_menu.htm", "Votedata/pntemplates/admin_header_menu.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/admin_list_district.htm", "Votedata/pntemplates/admin_list_district.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/admin_list_houseno.htm", "Votedata/pntemplates/admin_list_houseno.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/admin_list_person.htm", "Votedata/pntemplates/admin_list_person.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/admin_list_province.htm", "Votedata/pntemplates/admin_list_province.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/admin_list_tambon.htm", "Votedata/pntemplates/admin_list_tambon.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/admin_list_village.htm", "Votedata/pntemplates/admin_list_village.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/admin_std_footer.htm", "Votedata/pntemplates/admin_std_footer.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/ajax_list_district.htm", "Votedata/pntemplates/ajax_list_district.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/ajax_list_houseno.htm", "Votedata/pntemplates/ajax_list_houseno.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/ajax_list_tambon.htm", "Votedata/pntemplates/ajax_list_tambon.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/ajax_list_village.htm", "Votedata/pntemplates/ajax_list_village.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/admin_form_migrate.htm", "Votedata/pntemplates/admin_form_migrate.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/admin_list_migrate.htm", "Votedata/pntemplates/admin_list_migrate.htm"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/admin_view_migrate.htm", "Votedata/pntemplates/admin_view_migrate.htm"); 
    // Add pntemplates Folder
    $zip->addDir("Votedata/pntemplates/plugins"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/plugins/function.selector_object_array_ex.php", "Votedata/pntemplates/plugins/function.selector_object_array_ex.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pntemplates/plugins/function.yppager.php", "Votedata/pntemplates/plugins/function.yppager.php"); 
    // Add pnajax Folder
    $zip->addDir("Votedata/pnajax"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnajax/ajax_form_house_no.js", "Votedata/pnajax/ajax_form_house_no.js"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnajax/ajax_form_person.js", "Votedata/pnajax/ajax_form_person.js"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnajax/ajax_form_tambon.js", "Votedata/pnajax/ajax_form_tambon.js"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnajax/ajax_form_village.js", "Votedata/pnajax/ajax_form_village.js"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnajax/ajax_list_house_no.js", "Votedata/pnajax/ajax_list_house_no.js"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnajax/ajax_list_person.js", "Votedata/pnajax/ajax_list_person.js"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnajax/ajax_list_tambon.js", "Votedata/pnajax/ajax_list_tambon.js"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnajax/ajax_list_village.js", "Votedata/pnajax/ajax_list_village.js"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnajax/ajax_form_project.js", "Votedata/pnajax/ajax_form_project.js"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnajax/ajax_thread.js", "Votedata/pnajax/ajax_thread.js"); 

    // Add pnajax Folder
    $zip->addDir("Votedata/pnimages"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnimages/ajax-loader-2.gif", "Votedata/pnimages/ajax-loader-2.gif"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnimages/back.png", "Votedata/pnimages/back.png"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnimages/delete.png", "Votedata/pnimages/delete.png"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnimages/next.png", "Votedata/pnimages/next.png"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnimages/pencil.png", "Votedata/pnimages/pencil.png"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnimages/table_refresh.png", "Votedata/pnimages/table_refresh.png"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnimages/table_delete.png", "Votedata/pnimages/table_delete.png"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnimages/table_add.png", "Votedata/pnimages/table_add.png"); 

    // Add pnincludes Folder
    $zip->addDir("Votedata/pnincludes"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnincludes/DataUtilEx.class.php", "Votedata/pnincludes/DataUtilEx.class.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnincludes/DateUtilEx.class.php", "Votedata/pnincludes/DateUtilEx.class.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnincludes/DBUtilEx.class.php", "Votedata/pnincludes/DBUtilEx.class.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnincludes/DZip.class.php", "Votedata/pnincludes/DZip.class.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnincludes/HtmlUtilEx.class.php", "Votedata/pnincludes/HtmlUtilEx.class.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnincludes/InnoUtil.class.php", "Votedata/pnincludes/InnoUtil.class.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnincludes/ObjectUtilEx.class.php", "Votedata/pnincludes/ObjectUtilEx.class.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnincludes/PNObjectEx.class.php", "Votedata/pnincludes/PNObjectEx.class.php"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnincludes/PNObjectExArray.class.php", "Votedata/pnincludes/PNObjectExArray.class.php"); 

    // Add pnincludes/tab Folder
    $zip->addDir("Votedata/pnincludes/tab"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnincludes/tab/adobe.css", "Votedata/pnincludes/tab/adobe.css"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnincludes/tab/default.css", "Votedata/pnincludes/tab/default.css"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnincludes/tab/dropdown.css", "Votedata/pnincludes/tab/dropdown.css"); 

    // Add pnincludes/tab/images Folder
    $zip->addDir("Votedata/pnincludes/tab/images"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnincludes/tab/images/dropdown_pane.png", "Votedata/pnincludes/tab/images/dropdown_pane.png"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnincludes/tab/images/dropdown_pane_8bit.png", "Votedata/pnincludes/tab/images/dropdown_pane_8bit.png"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnincludes/tab/images/grad1.png", "Votedata/pnincludes/tab/images/grad1.png"); 
    $zip->addFile("modules/VoteDataCenter/modtemplate/pnincludes/tab/images/nav-arrow-right.png", "Votedata/pnincludes/tab/images/nav-arrow-right.png"); 

    // Add data Folder
    $zip->addDir("Votedata/data"); 
    $zip->addFile("pnTemp/votedatacenter_upload/province.csv", "Votedata/data/province.csv"); 
    $zip->addFile("pnTemp/votedatacenter_upload/district.csv", "Votedata/data/district.csv"); 
    $zip->addFile("pnTemp/votedatacenter_upload/tambon.csv", "Votedata/data/tambon.csv"); 
    $zip->addFile("pnTemp/votedatacenter_upload/village.csv", "Votedata/data/village.csv"); 
    $zip->addFile("pnTemp/votedatacenter_upload/house_no.csv", "Votedata/data/house_no.csv"); 
    $zip->addFile("pnTemp/votedatacenter_upload/person.csv", "Votedata/data/person.csv"); 
    $zip->save();


    /////////////////////////////////////////////
    //Redirect page
    /////////////////////////////////////////////
    $form_url = pnModURL('VoteDataCenter', 'admin', 'form' , array(
                                                                   'ctrl'=>$ctrl,
                                                                   'form[pro_id]' => $form['pro_id'],
                                                                   'form[dis_id]' => $form['dis_id'],
                                                                   'form[tam_id]' => $form['tam_id'],
                                                                   'filename'=>$filename
                                                             )
                );

    pnRedirect($form_url);
    
    return true;
}

  function VoteDataCenter_adminform_upload(){
    //$count = DBUtil::selectObjectCount('votedatacenter_files' , "WHERE img_advertise_id  = '$id'");

    if (in_array(0,$_FILES['images']['error'])){
      $files = $_FILES['images'];
      $filespath = "pnTemp/votedatacenter_upload/csv/";
      foreach ($files["error"] as $key => $error){
        if ($error == 0){
            $tmp_name = $files["tmp_name"][$key];
            $fliename = $files["name"][$key];

////////////////////////// get the extension of the file in a lower case format/////////////////////////////////////
            $filename = stripslashes($fliename);
            $i = strrpos($filename,".");
            if (!$i) { return ""; }
            $l = strlen($filename) - $i;
            $ext = substr($filename,$i+1,$l);
            $extension = strtolower($ext);

////////////////////////////generate new file&path//////////////////////////////////////////////////////////////////
            $rootname       = "[".time()."].".$extension;
            $filename_temp  = "images_".$id.$rootname;
            $imgpath        = $filespath.$filename_temp;

/////////////////////////Copy images to directory///////////////////////////////////////////////////////////////////
            copy($tmp_name,$imgpath);

            if(strcmp("jpg",$extension)==0)
              $src_img=imagecreatefromjpeg($tmp_name);

            if(strcmp("jpeg",$extension)==0)
              $src_img=imagecreatefromjpeg($tmp_name);

            if(strcmp("png",$extension)==0)
              $src_img=imagecreatefrompng($tmp_name);

            if(strcmp("gif",$extension)==0)
              $src_img=imagecreatefromgif($tmp_name);

            if(strcmp("bmp",$extension)==0)
              $src_img=imagecreatefromwbmp($tmp_name);

            //gets the dimmensions of the image
            $old_x=imageSX($src_img);
            $old_y=imageSY($src_img);

            // next we will calculate the new dimmensions for the thumbnail image
            // the next steps will be taken:
            // 1. calculate the ratio by dividing the old dimmensions with the new ones
            // 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable
            // and the height will be calculated so the image ratio will not change
            // 3. otherwise we will use the height ratio for the image
            // as a result, only one of the dimmensions will be from the fixed ones
            $new_w = 200;
            $new_h = 150;
            $ratio1=$old_x/$new_w;
            $ratio2=$old_y/$new_h;
            if($ratio1>$ratio2) {
              $thumb_w=$new_w;
              $thumb_h=$old_y/$ratio1;
            }else{
              $thumb_h=$new_h;
              $thumb_w=$old_x/$ratio2;
            }

            // we create a new image with the new dimmensions
            $dst_img   = ImageCreateTrueColor($thumb_w,$thumb_h);

            // resize the big image to the new created one
            $thumb = imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

            $thumbsize = '1000';
            $thumbtype = $filetype;

            // output the created image to the file. Now we will have the thumbnail into the file named by $filename
            if(strcmp("png",$extension)==0){
              imagepng($dst_img,$tmbpath);
            }else if(strcmp("jpg",$extension)==0){
              imagejpeg($dst_img,$tmbpath);
            }else if(strcmp("jpeg",$extension)==0){
              imagejpeg($dst_img,$tmbpath);
            }else if(strcmp("gif",$extension)==0){
              imagegif($dst_img,$tmbpath);
            }else if(strcmp("bmp",$extension)==0){
              imagewbmp($dst_img,$tmbpath);
            }else {
              imagejpeg($dst_img,$tmbpath);
            }

            //destroys source and destination images.
            imagedestroy($dst_img);
            imagedestroy($src_img);

            $objects = array(
                            'id'            => $next_id,
                            'path'          => $filespath,
                            'advertise_id'  => $id,
                            'imagename'     => $filename_temp,
                            'thumbname'     => $thumbname_temp, 
                            'status'        => 1
                           );

          DBUtil::insertObject($objects,'votedatacenter_files');

        }
        
      }
      //Set default images
      //if ($id && $count==0){
      if ($id){
        $object = array('firstfilename'=>$filename_temp , 'firstthumbname'=>$thumbname_temp);
        $where  = "adv_id = ".$id;
        DBUtil::updateObject($object,'innoadvert_advert',$where);
      }
    }
    $this->checkHasFile($id);
  }


  function VoteDataCenter_fixEncoding($in_str) {
    $cur_encoding = mb_detect_encoding($in_str) ;

    if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8"))
      return $in_str;
    else
      return utf8_encode($in_str);
  } 
?>